package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Comment;
import com.javaclimb.music.service.CommentService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 评论控制类
 * */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * */
    @PostMapping("/add")
    public Object addComment(Comment comment){
        System.out.println("add comment:" + comment);
        JSONObject jsonObject = new JSONObject();
        if(comment.getType() == 0){
            comment.setSongListId(null);
        }else{
            comment.setSongId(null);
        }
        boolean flag = commentService.insert(comment);
        if(flag){
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改评论
     * */
    @PostMapping("/update")
    public Object updateComment(Comment comment){
        System.out.println("update comment:" + comment);
        JSONObject jsonObject = new JSONObject();
        boolean flag = commentService.update(comment);
        if(flag){
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除评论
     * */
    @GetMapping("/delete")
    public Object deleteComment(Integer id){
        //return comment;
        boolean flag = commentService.delete(id);
        return flag;
    }

    /**
     * 根据主键查询整个对象
     * */
    @GetMapping("/selectByPrimaryKey")
    public Object selectByPrimaryKey(Integer id){
        //return comment;
        return commentService.selectByPrimaryKey(id);
    }

    /**
     * 查询所有评论
     * */
    @GetMapping("/allComment")
    public Object selectAllComment(){
        //return comment;
        return commentService.allComment();
    }

    /**
     * 查询某个歌曲下的所有评论
     * */
    @GetMapping("/commentOfSongId")
    public Object commentOfSongId(Integer songId){
        return commentService.commentOfSongId(songId);
    }

    /**
     * 查询某个歌单下的所有评论
     * */
    @GetMapping("/commentOfSongListId")
    public Object commentOfSongListId(Integer songListId){
        return commentService.commentOfSongListId(songListId);
    }

    /**
     * 给某个评论点赞
     * */
    @PostMapping("/like")
    public Object like(Integer id){
        System.out.println("update up:" + id);
        JSONObject jsonObject = new JSONObject();
        boolean flag = commentService.updateUp(id);
        if(flag){
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "点赞成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "点赞失败");
        return jsonObject;
    }




}
